/*Benny Vermonden c1025695*/

package views.searchViews;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.ListSelectionModel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JList;
import javax.swing.JButton;

import core.logic.services.utilServices.ValidatieService;
import core.models.dataModels.searchModels.ZoekMaatschappijModel;
import core.models.domainModel.Adres;
import core.models.domainModel.Maatschappij;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import controllers.searchControllers.ZoekMaatschappijController;
import controllers.updateControllers.UpdateMaatschappijController;

public class ZoekMaatschappijView extends JFrame {
	private static final long serialVersionUID = 1L;
	private JTextField txt_naam;
	private JTextField txt_straat;
	private JTextField txt_huisnummer;
	private JTextField txt_postcode;
	private JTextField txt_gemeente;
	private JTextField txt_land;
	private ZoekMaatschappijModel _model;
	
	private JList _gevondenMaatschappijJList;
	
	private DefaultListModel listModel = new DefaultListModel();
	public ZoekMaatschappijView(ZoekMaatschappijModel model) {
		_model = model;
		
		JLabel lblMaatschappijZoeken = new JLabel("Maatschappij zoeken:");
		
		JLabel lblMaatschappijnaam = new JLabel("Maatschappijnaam:");
		
		txt_naam = new JTextField();
		txt_naam.setColumns(10);
		
		JLabel label_2 = new JLabel("Straat:");
		
		txt_straat = new JTextField();
		txt_straat.setColumns(10);
		
		JLabel label_3 = new JLabel("Huisnummer:");
		
		txt_huisnummer = new JTextField();
		txt_huisnummer.setColumns(10);
		
		JLabel label_4 = new JLabel("Postcode:");
		
		txt_postcode = new JTextField();
		txt_postcode.setColumns(10);
		
		JLabel label_5 = new JLabel("Gemeente:");
		
		txt_gemeente = new JTextField();
		txt_gemeente.setColumns(10);
		
		JLabel label_6 = new JLabel("Land:");
		
		txt_land = new JTextField();
		txt_land.setColumns(10);
		
		JButton btnZoeken = new JButton("Zoeken");
		btnZoeken.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				listModel.clear();
				
				
				
					_model.setMaatschappijnaam(txt_naam.getText());
					_model.setGemeente(txt_gemeente.getText());
					_model.setHuisnummer(txt_huisnummer.getText());
					_model.setLand(txt_land.getText());
					_model.setPostcode(txt_postcode.getText());
					_model.setStraat(txt_straat.getText());
					
					
					ZoekMaatschappijController.handleView(_model);
					for(Maatschappij item : _model.getGevondenMaatschappijen())
					{
						listModel.addElement(item);
					}
				
				
				
			}
		});
		
		JButton btnWisZoekcriteria = new JButton("Wis zoekcriteria");
		btnWisZoekcriteria.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				txt_gemeente.setText("");
				txt_huisnummer.setText("");
				txt_land.setText("");
				txt_naam.setText("");
				txt_postcode.setText("");
				txt_straat.setText("");
			}
		});
		
		JLabel lblGevondenEigenaars = new JLabel("Gevonden maatschappijen:");
		_gevondenMaatschappijJList = new JList(listModel);
		_gevondenMaatschappijJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		JButton btnWijzigMaatschappij = new JButton("Wijzig Maatschappij");
		btnWijzigMaatschappij.addActionListener(new ActionListener() {
			@SuppressWarnings("unused")
			public void actionPerformed(ActionEvent arg0) {
				
				final Maatschappij updateMaatschappij = (Maatschappij)_gevondenMaatschappijJList.getSelectedValue();
//				updateMaatschappij.setEigenaarID(((Maatschappij)_gevondenMaatschappijJList.getSelectedValue()).getEigenaarID());
				//als er een Maatschappij geselecteerd is
				if(updateMaatschappij != null)
				{
//					
					UpdateMaatschappijController.showUpdateMaatschappijView(updateMaatschappij);;					
				}
				else{
					//melding eerst een Maatschappij selecteren
					JOptionPane.showMessageDialog(
							null,
							"Selecteer eerst een maatschappij uit de lijst.",
							"Selecteer Personeel.",
							JOptionPane.INFORMATION_MESSAGE);
				}
				
			}
		});
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addComponent(lblMaatschappijZoeken)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblMaatschappijnaam)
								.addComponent(label_2)
								.addComponent(label_3)
								.addComponent(label_4)
								.addComponent(label_5)
								.addComponent(label_6))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(txt_naam, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE)
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(1)
									.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
										.addComponent(txt_straat, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 169, GroupLayout.PREFERRED_SIZE)
										.addComponent(txt_gemeente, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE)
										.addComponent(txt_land, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)
										.addGroup(Alignment.LEADING, groupLayout.createParallelGroup(Alignment.TRAILING, false)
											.addComponent(txt_postcode, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
											.addComponent(txt_huisnummer, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE))))))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnZoeken)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnWisZoekcriteria)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnWijzigMaatschappij)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblGevondenEigenaars))
						.addComponent(_gevondenMaatschappijJList, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addContainerGap(160, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblMaatschappijZoeken)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txt_naam, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblMaatschappijnaam))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txt_straat, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_2))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txt_huisnummer, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_3))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txt_postcode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_4))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txt_gemeente, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_5))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txt_land, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_6))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnZoeken)
						.addComponent(btnWisZoekcriteria)
						.addComponent(btnWijzigMaatschappij)
						.addComponent(lblGevondenEigenaars))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(_gevondenMaatschappijJList, GroupLayout.PREFERRED_SIZE, 183, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(131, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
	}
}

